package com.lagou.resume.controller;

import com.alibaba.fastjson.JSON;
import com.lagou.resume.pojo.User;
import com.lagou.resume.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户管理的控制类
 * @autor yangzhenhao
 */
@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户登录的方法
     * @param modelAndView
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest req, HttpServletResponse resp, ModelAndView modelAndView) {
        String userid = req.getParameter("userid");
        String password = req.getParameter("password");
        User userInfo = new User();
        userInfo.setUserid(userid);
        userInfo.setPassword(password);
        User user = userService.login(userInfo);
        Map<String,String> map = new HashMap<>();
        if(user == null) {
            map.put("code","1");
        } else {
            map.put("code","0");
            req.getSession().setAttribute("USER_SESSION",user);
        }
        return JSON.toJSONString(map);
    }

}